import os,uuid
from app import create_app
from werkzeug.exceptions import HTTPException
from flask import request,g

app = create_app(os.getenv('FLASK_CONFIG') or 'default')

# 定义一个前拦截器，可根据需要扩展
@app.before_request
def before_request():
    g.trace_id = str(uuid.uuid4().hex)
    url = request.url
    app.logger.info("请求地址：%s", url)
    
 
# 定义一个后拦截器，可根据需要扩展
@app.after_request
def after_request(response):
    app.logger.info("响应拦截器")
    return response

# 全局异常处理，可根据需要扩展
@app.errorhandler(Exception)
def handle_exception(e):
    app.logger.error('%s', e)
    if(isinstance(e, HTTPException)):
        return {'code':e.code,'message':e.description}, e.code
    else:
        return {'message':str(e)}, '500'
